package controller;

import com.jfinal.core.Controller;
import model.Student;

import java.util.List;


public class StudentController extends Controller {
    public void regester(){
        renderFreeMarker("register.ftl");
    }
    public void logincheck(){
        String username = getPara("name");
        String password = getPara("password");

        // 这个是有关于session的设置，再与拦截器一起完成登录主页拦截的作用。
        setSessionAttr("name",username);
        setSessionAttr("password",password);

        // redirect("/student/main");*/
        List<Student> students = Student.dao.find("select * from t_student where name = ? and password = ?",username,password);
        if(students.size()!=0){
            //renderHtml("主页面");
            redirect("/student/main");
        }else{
            setAttr("errmsg","用户名或密码错误");
            renderFreeMarker("login.ftl");
        }

    }
    public void login(){
        //setAttr("errmag","");
        renderFreeMarker("login.ftl");
    }
    public void save(){
        /*String name = getPara("name");
        String password = getPara("password");
        String gender = getPara("gender");
        String hometown = getPara("hometown");
        Student student = new Student();
        student.setName(name).setPassword(password).setGender(gender).setHometown(hometown);*/
        Student model = getModel(Student.class, "", true);

        model.save();
        //renderHtml("注册成功。");
        //setAttr("errmag","");
        // redirect("/student/login");
        renderFreeMarker("login.ftl");
    }
    //s@Before(main2_interceptor.class)
    public void main(){
        String username = getPara("username","");
        List<Student> students;
        if(username.isEmpty()){
            // String name = getSessionAttr("name");
            students = Student.dao.find("select * from t_student");
        }else{
            students = Student.dao.find("select * from t_student where username =?",username);
        }
        String name = getSessionAttr("name");
        //List<Student> students = Student.dao.find("select * from t_student");
        setAttr("students",students);
        setAttr("name",name);
        renderFreeMarker("index.ftl");
    }
}
